package dr.util;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import com.oreilly.servlet.MultipartRequest;
import com.oreilly.servlet.multipart.DefaultFileRenamePolicy;

public class FileUtil {
	
	public static final String UPLOAD_PATH = "C:/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/drBank/fileSave";
	public static final String  ENCODING_TYPE = "euc-kr";
    public static final int MAX_SIZE = 10*1024*1024;//10M
	
    public static  MultipartRequest createFile(HttpServletRequest request) throws IOException{
		return new MultipartRequest(request,UPLOAD_PATH,FileUtil.MAX_SIZE,ENCODING_TYPE,new DefaultFileRenamePolicy());
    }
    
    public static String rename(String filename, int no, String param) throws Exception{
		File file = new File(UPLOAD_PATH,filename);
		//파일명을 원하는 형식으로 변경하기
        int idx = filename.lastIndexOf(".");
        String extention = "";
        String newFileName = "";
        
        if(idx != -1) {
            extention = filename.substring(idx);
        }
        if(param.equals("item_img")){
        	newFileName = no + extention.toLowerCase();
        }
        File fs = new File(UPLOAD_PATH,newFileName);
        file.renameTo(fs);
        
		return newFileName;
	}
	public static void removeFile(String filename){
		File file = new File(UPLOAD_PATH,filename);
        if(file.exists()) file.delete();
	}
}